<?php
declare (strict_types = 1);
namespace app\validate;

use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     */
    protected $rule = [
        'username'      => 'require|max:30',
        'password'      => 'require|max:18|min:6',
        'old_password'  => 'max:18|min:6',
        'captcha'       => 'require|captcha'
    ];

    /**
     * 定义错误信息
     */
    protected $message = [
        'username.require'          => '请输入账号',
        'username.max'              => '账号不能超过30位字符',
        'password.require'          => '请输入密码',
        'password.max'              => '密码不能超过18位字符',
        'password.min'              => '密码不能小于6位字符',
        'old_password.max'          => '密码不能超过18位字符',
        'old_password.min'          => '密码不能小于6位字符',
        'old_password.requireWith'  => '修改密码则旧密码不能为空',
        'captcha.require'           => '请输入验证码',
        'captcha.captcha'           => '验证码错误',
    ];

    /**
     * 登录场景验证
     */
    public function sceneLogin()
    {
        return $this->only(['username', 'password', 'captcha']);
    }

    public function sceneProfile()
    {
        return $this->only(['username', 'password', 'old_password'])
            ->remove('password', 'require')
            ->append('old_password', 'requireWith:password');
    }
}
